package com.lyy.photovoltaic.service.impl;

import com.lyy.photovoltaic.beans.UserCart;
import com.lyy.photovoltaic.common.utils.MyRandom;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.mapper.UserCartMapper;
import com.lyy.photovoltaic.service.UserCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UserCartServiceImpl implements UserCartService {
    @Autowired
    UserCartMapper userCartMapper;
    @Override
    public ResultVO addUserCart(String userId) {
        String uCId = MyRandom.getStringRandom()+userId;
//        System.out.println(uCId);
        UserCart userCart = new UserCart();
        userCart.setUserCartId(uCId);
        userCart.setUserId(userId);
        userCart.setUcCreatetime(new Date());
        int i = userCartMapper.insert(userCart);
        if (i > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS,"创建购物车成功",userCart);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"创建购物车失败",null);
    }

    @Override
    public ResultVO getUserCartId(String userId) {
        Example example = new Example(UserCart.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("payStatus",0);
        UserCart userCart = userCartMapper.selectOneByExample(example);
        if (userCart == null) {
            return new ResultVO(ResultCode.FAILED,"查看购物车Id失败",null);
        }
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询购物车Id成功",userCart.getUserCartId());
        return resultVO;
    }
}
